package com.mybatis.jpa.executor;

import com.mybatis.jpa.constants.EnumAction;
import com.mybatis.jpa.sqlbuilder.InsertSqlBuilder;
import com.mybatis.jpa.sqlbuilder.QuerySqlBuilder;
import com.mybatis.jpa.sqlbuilder.SqlBuilder;

import java.util.HashMap;
import java.util.Map;

public class ExecutorFactory {
	
	private static Map<String, Executor> instanceMap = new HashMap<>();
	private static Map<String, Class<?>> factoryMap = new HashMap<>();
	private static Map<String, Class<?>> sqlBuilderMap = new HashMap<>();
		
	static {	
		factoryMap.put(EnumAction.INSERT.getKey(), PersistentExecutor.class);
		factoryMap.put(EnumAction.QUERY.getKey(), QueryExecutor.class);

		sqlBuilderMap.put(EnumAction.INSERT.getKey(), InsertSqlBuilder.class);
		sqlBuilderMap.put(EnumAction.QUERY.getKey(), QuerySqlBuilder.class);
	}
	
	private ExecutorFactory(){}

	public static Executor getInstance(String key) throws Exception {
		if (instanceMap.containsKey(key)) {
			return instanceMap.get(key);
		}

		Executor executor = (Executor) factoryMap.get(key).newInstance();
		executor.setSqlBuilder((SqlBuilder) sqlBuilderMap.get(key).newInstance());
		instanceMap.put(key, executor);
		return executor;
	}
}
